package org.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import jdk.jshell.Snippet;
import org.example.demo.constant.StatusConstant;
import org.example.demo.entity.ApplicationData;
import org.example.demo.entity.PartyApplication;
import org.example.demo.entity.User;
import org.example.demo.service.ApplicationDataService;
import org.example.demo.service.PartyApplicationService;
import org.example.demo.util.AjaxResult;
import org.example.demo.util.JWTUtils;
import org.example.demo.util.ReturnMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("party/application")
public class PartyApplicationController {

    @Resource
    private PartyApplicationService partyApplicationService;

    @Autowired
    private ApplicationDataService applicationDataService;

    // 分页查询入党申请书列表
    @PostMapping("selectPage")
    public AjaxResult selectPage(@RequestBody PartyApplication application,
                                 @RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<PartyApplication> page = new Page<>(pageNum, pageSize);

        QueryWrapper<PartyApplication> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(null != application.getName() && !application.getName().isEmpty(),
                        PartyApplication::getName, application.getName()).eq(application.getStatus()!=null, PartyApplication::getStatus,application.getStatus())
                .orderByDesc(PartyApplication::getCreateTime);

        Page<PartyApplication> result = partyApplicationService.page(page, queryWrapper);

        Map<String, Object> map = new HashMap<>();
//        returnMap.setData("page", result);
        map.put("page", result);
        return AjaxResult.success(map);
    }

    // 查询入党申请书总数
    @PostMapping("selectCount")
    public AjaxResult selectCount(@RequestBody PartyApplication application) {
//        ReturnMap returnMap = new ReturnMap();
        QueryWrapper<PartyApplication> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(null != application.getName() && !application.getName().isEmpty(),
                        PartyApplication::getName, application.getName());


        int count = partyApplicationService.count(queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("count", count);
//        returnMap.setData("count", count);
        return AjaxResult.success(map);
    }

    // 提交入党申请书
    @Transactional
    @PostMapping("submit")
    public AjaxResult submit(@RequestBody PartyApplication application, HttpServletRequest request) {
//        ReturnMap returnMap = new ReturnMap();
//        if (partyApplicationService.count(new QueryWrapper<PartyApplication>().lambda().eq(PartyApplication::getUserId,application.getUserId()))>0){
//            returnMap.setReturnCode(400);
//            return returnMap.getreturnMap();
//        }
        if (application.getId() != null) {
            application.setUpdateTime(LocalDateTime.now());
            partyApplicationService.updateById(application);
        } else {
            application.setCreateTime(LocalDateTime.now());
            application.setUpdateTime(LocalDateTime.now());
            application.setApplyDate(LocalDate.now());
            partyApplicationService.save(application);

            // 插入申请书审核数据
            ApplicationData applicationData = ApplicationData.builder().
                    applicationId(application.getId()).
                    status(StatusConstant.PENGING_REVIEW).
                    build();
            applicationDataService.save(applicationData);
        }

//        returnMap.setReturnCode(200);
        return AjaxResult.success();
    }

    // 查询入党申请书详情
    @PostMapping("detail")
    public AjaxResult detail(@RequestParam Integer id) {
//        ReturnMap returnMap = new ReturnMap();
        PartyApplication application = partyApplicationService.getById(id);

        Map<String, Object> map = new HashMap<>();
//        returnMap.setData("application", application);
        map.put("application", application);
        return AjaxResult.success(map);
    }


    // 删除入党申请书
    @Transactional
    @DeleteMapping("delete")
    public AjaxResult delete(@RequestParam Integer id) {
//        ReturnMap returnMap = new ReturnMap();
        boolean success = partyApplicationService.removeById(id);

        if (success) {
            applicationDataService.deleteByApplicationId(id);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }


    @GetMapping("/select")
    public AjaxResult getById(@RequestParam("id") Long id) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", id);

        List<PartyApplication> partyApplications = partyApplicationService.listByMap(map);

        Map<String, Object> maps = new HashMap<>();
        maps.put("partyApplications", partyApplications);
        return AjaxResult.success(maps);
    }
}